import http from '@/utils/http'
import type { ApiResponse } from '@/types/api';
import type { Message, MessageListResponse } from '@/types/message';

// 医生预约请求参数接口
export interface DoctorAppointmentRequest {
    /**
     * 医生id
     */
    doctorId: string;
    /**
     * 结束日期，不传默认为当天
     */
    endDate?: string;
    /**
     * 名称模糊查询
     */
    name?: string;
    /**
     * 页码
     */
    pageNum?: string;
    /**
     * 每页大小
     */
    pageSize?: string;
    /**
     * 开始日期
     */
    startDate?: string;
    /**
     * 预约状态查询(PENDING:待处理, REJECTED:已拒绝,TIMEOUT:已超时)
     */
    status?: string;
    [property: string]: any;
}

// 医生预约信息接口
export interface DoctorAppointmentVO {
    /**
     * 预约ID
     */
    id: number;
    /**
     * 患者用户ID
     */
    userId: number;
    /**
     * 患者姓名
     */
    name: string;
    /**
     * 患者电话
     */
    phone: string;
    /**
     * 预约状态
     */
    status: string;
    /**
     * 过期时间
     */
    expireTime: string;
    /**
     * 创建时间
     */
    createTime: string;
    /**
     * 拒绝时间
     */
    rejectTime: string | null;
    [property: string]: any;
}

// 分页响应接口
export interface DoctorAppointmentPageResponse {
    records: DoctorAppointmentVO[];
    total: number;
    [property: string]: any;
}

// 获取医生预约列表
export const getDoctorAppointments = (params: DoctorAppointmentRequest): Promise<ApiResponse<DoctorAppointmentPageResponse>> => {
    return http({
        url: '/doctor/appointment/list',
        method: 'GET',
        params
    })
}

// 接受预约
export const acceptAppointment = (appointmentId: number): Promise<ApiResponse<any>> => {
    return http({
        url: `/doctor/appointment/accept`,
        method: 'PUT',
        params: { appointmentId }
    })
}

// 拒绝预约
export const rejectAppointment = (appointmentId: number): Promise<ApiResponse<any>> => {
    return http({
        url: `/doctor/appointment/reject`,
        method: 'PUT',
        params: { appointmentId }
    })
}

// 删除预约请求接口
export interface DeleteAppointmentRequest {
    appointmentIds: number[];
    [property: string]: any;
}

// 删除预约
export const deleteAppointment = (data: DeleteAppointmentRequest): Promise<ApiResponse<any>> => {
    return http({
        url: '/doctor/appointment',
        method: 'DELETE',
        data
    })
}

// 获取预约详情
export const getAppointmentDetail = (appointmentId: number): Promise<ApiResponse<DoctorAppointmentVO>> => {
    return http({
        url: `/doctor/appointment/detail/${appointmentId}`,
        method: 'GET'
    })
}

// 获取医生预约消息
export const getDoctorAppointmentMessages = (params?: {
    page?: number;
    pageSize?: number;
    status?: number; // 消息状态：0-未读，1-已读
}): Promise<ApiResponse<MessageListResponse>> => {
    return http({
        url: '/doctor/appointment/msg',
        method: 'GET',
        params
    })
}

// 标记所有预约消息为已读
export const readAllDoctorAppointmentMessages = (): Promise<ApiResponse<any>> => {
    return http({
        url: '/doctor/appointment/msg/read-all',
        method: 'POST'
    })
}

// 标记单个预约消息为已读
export const readDoctorAppointmentMessage = (msgId: number): Promise<ApiResponse<any>> => {
    return http({
        url: `/doctor/appointment/msg/read/${msgId}`,
        method: 'POST'
    })
}

// 删除预约消息
export const deleteDoctorAppointmentMessage = (msgId: number): Promise<ApiResponse<any>> => {
    return http({
        url: `/doctor/appointment/msg/delete/${msgId}`,
        method: 'DELETE'
    })
}
